<template>
	<view class="uni-padding-wrap">
		<form @submit="formSubmit" @reset="formReset">
			<view class="uni-form-item uni-column">
				<view class="title"><text class="requiedF">*</text>企业号</view>
				<input class="uni-input" name="company" placeholder="请输入企业号" ></input>
			</view>
			<view class="uni-form-item uni-column">
				<view class="title">部门编号</view>
				<input class="uni-input" name="department" placeholder="请输入部门编号" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title"><text class="requiedF">*</text>工号</view>
				<input class="uni-input" name="jobnumber" placeholder="请输入工号" />
			</view>
			<view class="uni-form-item uni-column">
				<view class="title"><text class="requiedF">*</text>姓名</view>
				<input class="uni-input" name="name" placeholder="请输入姓名" />
			</view>
			<view class="uni-btn-v">
				<button form-type="submit" type='primary'>提交</button>
				<button type="default" form-type="reset">重置</button>
			</view>
		</form>
	</view>
</template>

<script>
	var graceChecker = require('common/graceChecker.js');
	const app = getApp();
	export default {
		data() {
			return {

			}
		},
		methods: {
			formSubmit: function(e) {
				console.log('form发生了submit事件，携带数据为：' + JSON.stringify(e.detail.value))
				//定义表单规则
				var rule = [{
						name: "company",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请填写企业号"
					},
					{
						name: "jobnumber",
						checkType: "notnull",
						checkRule: "",
						errorMsg: "请填写工号"
					},
					{
						name: "name",
						checkType: "string",
						checkRule: "1,4",
						errorMsg: "姓名应为1-4个字符"
					}
				];
				//进行表单检查
				var formData = e.detail.value;
			
				var checkRes = graceChecker.check(formData, rule);
				if (!checkRes) {
					uni.showToast({
						title: graceChecker.error,
						icon: "none"
					});
				} else {
					if (!app.globalData.token) {
						uni.reLaunch({
							url: '../../index/index'
						});
						return;
					}
					uni.showLoading({
						mask: true,
						title: '处理中...'
					});
					uniCloud.callFunction({
						name: 'company_wx_auth',
						data: {
							reqType: 3,
							token: app.globalData.token,
							...formData
						}
					}).then((res) => {
						uni.hideLoading()
						if (res.result.status) {
							app.globalData.employee = res.result.employee;
							uni.showToast({
								title: '绑定成功',
								icon: 'success'
							});
							uni.reLaunch({
								url: '../../form/form'
							});
						} else {
							uni.showToast({
								title: `绑定失败：${res.result.msg}`,
								icon: 'none'
							});
						}
					}).catch((err) => {
						uni.hideLoading()
						uni.showToast({
							title: `绑定失败：${err.message}`,
							icon: 'none'
						});
					})
				}
			},
			formReset: function(e) {
				console.log('清空数据')
			}
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 10rpx 0;
	}
	.requiedF {
		color: #FF3333;
	}
</style>
